package com.kum.service;

import com.kum.domain.dto.RegisterDto;
import com.kum.domain.entity.SysFaceData;
import com.kum.mapper.SysUserMapper;
import com.kum.domain.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version V1.0
 * @Package com.kum.service
 * @auhter 枯木Kum
 * @date 2021/3/9-6:25 PM
 */

@Service
public class SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysFaceDataService sysFaceDataService;


    public SysUser findById(Integer id){
        return sysUserMapper.selectById(id);
    }

    public List<SysUser> list(){
        return sysUserMapper.selectList(null);
    }

    /**
     * 加入用户
     * @param sysUser
     * @return 用户ID
     */
    public Integer add(SysUser sysUser){
        sysUserMapper.insert(sysUser);
        return sysUser.getId();
    }

    /**
     * 注册一个用户,并存储人脸数据
     * @param registerDto
     */
    public void register(RegisterDto registerDto){
        SysUser sysUser = registerDto.getSysUser();
        Date date = new Date();
        add(sysUser);
        SysFaceData sysFaceData = new SysFaceData()
                .toBuilder()
                .userId(sysUser.getId())
                .faceData(registerDto.getFaceData())
                .createTime(date)
                .updateTime(date)
                .build();
        sysFaceDataService.save(sysFaceData);


    }

}
